package com.shopping.mall.core.module.mainPage.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopping.mall.core.module.mainPage.entity.MainPage;
import com.shopping.mall.core.module.mainPage.entity.MainPageVo;
import com.shopping.mall.core.module.mainPage.mapper.MainPageMapper;
import com.shopping.mall.core.module.mainPage.service.MainPageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shopping.mall.base.http.ResponseState;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.base.util.WrapperUtils;
import io.jsonwebtoken.ExpiredJwtException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
 * <p>
 *  MainPage服务实现类
 * </p>
 *
 * @author zzy
 * @since 2021-09-02
 */
@Validated
@Service
public class MainPageServiceImpl extends ServiceImpl<MainPageMapper, MainPage> implements MainPageService {

    @Autowired
    private MainPageMapper mainPageMapper;

    /**
     * 添加
     *
     * @param mainPageVo
     * @return
     */
    @Override
    public Result<Object> add(MainPageVo mainPageVo) {
        MainPage mainPage=new MainPage(
                JSON.toJSONString(mainPageVo.getMainSlideshows()),
                JSON.toJSONString(mainPageVo.getSecondaryImages()),
                JSON.toJSONString(mainPageVo.getSideImages()),
                JSON.toJSONString(mainPageVo.getLeftText()),
                JSON.toJSONString(mainPageVo.getRightText()),
                mainPageVo.getExampleGoodsId(),
                mainPageVo.getSelfDefineModules(),
                mainPageVo.getLogo()
        );
        final int insert = mainPageMapper.insert(mainPage);
        if (insert==0){
            return Result.error(ResponseState.ERROR.getMessage());
        }
        return Result.OK();
    }

    /**
     * 返回最新的
     *
     * @return
     */
    @Override
    public Result<Object> select() {
        final MainPage mainPage = mainPageMapper.lastData();
        return Result.OK(mainPage);
    }


    /**
     * 分页查询
     * @param current：页码
     * @param size：每页条数
     * @param mainPage：筛选条件
     * @return IPage<MainPage>
     */
    @Override
    public IPage<MainPage> getMainPagePage(Integer current,Integer size,MainPage mainPage){
        QueryWrapper<MainPage> queryWrapper = WrapperUtils.queryWrapper(mainPage);
        Page<MainPage> mainPagePage = new Page<>(current,size);
        IPage<MainPage> mainPageIPage = mainPageMapper.selectPage(mainPagePage, queryWrapper);
        return mainPageIPage;
    }

    /**
     * 查询所有
     * @param mainPage：筛选条件
     * @return List<MainPage>
     */
    @Override
    public List<MainPage> getAll(MainPage mainPage){
        QueryWrapper<MainPage> queryWrapper = WrapperUtils.queryWrapper(mainPage);
        return mainPageMapper.selectList(queryWrapper);
    }

    /**
     * 通过id删除
     * @param id：mainPageId
     * @return Result<Object>
     */
    @Override
    public Result<Object> deleteById(String id){
        MainPage mainPage=mainPageMapper.selectById(id);
        if(mainPage==null){
            return Result.error(ResponseState.TARGET_NOT_EXIST.getValue(), ResponseState.TARGET_NOT_EXIST.getMessage());
        }
        mainPageMapper.deleteById(id);
        return Result.OK();
    }

    /**
    * 批量删除
    * @param ids：id列表
    * @return Result<Object>
    */
    @Override
    public Result<Object> batchDelete(List<String> ids){
        UpdateWrapper<MainPage> updateWrapper=new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        mainPageMapper.delete(updateWrapper);
        return Result.OK();
    }
}